package edu.uta.ui;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import edu.uta.ui.R;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import edu.uta.beans.MapDataBean;
import edu.uta.components.BalloonOverlayItem;
import edu.uta.components.MapMarker;
import edu.uta.parsers.NewParser;

/**
 *  @author fall 2011 team5
 */
/*
 * References
 *   http://developer.android.com/resources/tutorials/views/hello-mapview.html
 *   http://about-android.blogspot.com/2010/03/sample-google-map-driving-direction.html
 */
public class AQIMap extends MapActivity implements Caller {
	/**
	 * Called when the activity is first created.
	 * @uml.property  name="myMapView"
	 * @uml.associationEnd  
	 */

	AQIMap ref;
	MapView myMapView = null;
	/**
	 * @uml.property  name="myMC"
	 * @uml.associationEnd  
	 */
	MapController myMC = null;
	/**
	 * @uml.property  name="geoPoint"
	 * @uml.associationEnd  
	 */
	GeoPoint geoPoint = null;
	/**
	 * @uml.property  name="mapOverlays"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="uta.project.components.MapMarker"
	 */
	List<Overlay> mapOverlays;
	/**
	 * @uml.property  name="drawable"
	 * @uml.associationEnd  
	 */
	Drawable drawable;
	/**
	 * @uml.property  name="imageOnBallon"
	 * @uml.associationEnd  
	 */
	Drawable imageOnBallon;

	Drawable imageOnBallonForecast;


	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_layout);
		//link variable to resource by using id
		myMapView = (MapView) findViewById(R.id.myMapView);

		geoPoint = null;
		myMapView.setSatellite(false);
		ref= this;
		//getAQIdata return arraylist of mapdatabeans

		//ArrayList<MapDataBean> mapDataList = KMLParser.getAQIData("http://www.epa.gov/airnow/today/airnow_today.kml");
		// ArrayList<MapDataBean> forecastDataList = KMLParser.getAQIData("http://www.epa.gov/airnow/today/airnow_tomorrow.kml");
		//Log.v("datasize", mapDataList.size()+"");
		//  Log.v("forecastDataList", forecastDataList.size()+"");

		new NewParser(AQIMap.this,ref).execute();
		
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}


	public void postRequestHandler(Hashtable <String, MapDataBean> hTable){


		mapOverlays = myMapView.getOverlays();
		Set<String> set = hTable.keySet();
		String str;
		Iterator<String> itr = set.iterator();
		while (itr.hasNext()) {
			str = itr.next();
			//System.out.println(str + ": " + hTable.get(str));

			MapDataBean dataBean = hTable.get(str);

			if(dataBean.getAirQuality().contains("Good"))
			{
				//drawable- image to be displayed as marker on the map
				drawable = this.getResources().getDrawable(R.drawable.aqi1circle);
				//imageOnBallon image on callout
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi0);
			}
			else if(dataBean.getAirQuality().contains("Moderate"))
			{
				drawable = this.getResources().getDrawable(R.drawable.aqi2circle);
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi1);
			}
			else if(dataBean.getAirQuality().contains("UnhealthySG"))
			{
				//dataBean.setAirQuality("Unhealthy for Sensitive Groups");
				drawable = this.getResources().getDrawable(R.drawable.aqi3circle);
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi2);
			}
			else if(dataBean.getAirQuality().contains("Unhealthy"))
			{
				drawable = this.getResources().getDrawable(R.drawable.aqi4circle);
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi3);
			}
			else if(dataBean.getAirQuality().contains("Very Unhealthy"))
			{
				drawable = this.getResources().getDrawable(R.drawable.aqi5circle);
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi4);
			}
			else if(dataBean.getAirQuality().contains("Hazardous"))
			{
				drawable = this.getResources().getDrawable(R.drawable.aqi6circle);
				imageOnBallon=  this.getResources().getDrawable(R.drawable.aqi5);
			}
			else
			{
				drawable = this.getResources().getDrawable(R.drawable.aqi0circle);
				imageOnBallon=drawable;
			}

			//Log.v("dataBean", dataBean.toString());

			if(dataBean.getForecastAQI().contains("Good"))
			{
				//imageOnBallon image on callout
				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi0);
			}
			else if(dataBean.getForecastAQI().contains("Moderate"))
			{

				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi1);
			}
			else if(dataBean.getForecastAQI().contains("UnhealthySG"))
			{
				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi2);
			}
			else if(dataBean.getForecastAQI().contains("Unhealthy"))
			{

				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi3);
			}
			else if(dataBean.getForecastAQI().contains("Very Unhealthy"))
			{
				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi4);
			}
			else if(dataBean.getForecastAQI().contains("Hazardous"))
			{
				imageOnBallonForecast=  this.getResources().getDrawable(R.drawable.aqi5);
			}
			
			else
			{
				imageOnBallonForecast=null;
			}
			

			//Log.v("desc", description);
			//create a marker
			MapMarker marker = new MapMarker(drawable,myMapView);		
			//create points on the map where markers are to be placed based on lat-long info
			geoPoint = new GeoPoint((int)(dataBean.getLatitude()*1E6),(int)(dataBean.getLongitude()*1E6));
			//create new callout for each point on the map
			BalloonOverlayItem overlayitem = new BalloonOverlayItem(geoPoint, str+"\n"+dataBean.getAirQuality(),dataBean.getPlace(), dataBean.getForecastAQI(),dataBean.getForecastDesc(),imageOnBallon,imageOnBallonForecast);

			//add callout to the marker
			marker.addOverlay(overlayitem);
			//Add markers to map overlay list
			mapOverlays.add(marker);
		}


		//setting the appearance of the map
		myMC = myMapView.getController();
		myMC.setZoom(5);
		//to display zoom buttons
		/*Projection p = myMapView.getProjection(); 
		GeoPoint point = p.fromPixels(10, 5);
		myMC.animateTo(point);*/

		myMapView.setBuiltInZoomControls(true);
		myMapView.getController().animateTo(new GeoPoint((int)(40.3347*1E6),(int)(-103.7109*1E6)));



	}


}